<template>
  <div class="login-container">
    <div class="left-panel">
      <div class="slogan">
        <h1>为智慧工厂数字化赋能</h1>
        <h2>做企业数字化转型合作伙伴</h2>
      </div>
      <div class="factory-image">
        <img src="../assets/Factory.png" alt="" />
      </div>
    </div>
    <div class="right-panel">
      <div class="login-form">
        <h2>欢迎登录</h2>
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" label-position="top">
          <el-form-item prop="JobNumber">
            <el-input
              v-model="loginForm.JobNumber"
              type="text"
              auto-complete="off"
              placeholder="账号"
              prefix-icon="User"
              size="large"
            ></el-input>
          </el-form-item>

          <el-form-item prop="passWord">
            <el-input
              v-model="loginForm.passWord"
              prefix-icon="Lock"
              type="password"
              placeholder="密码"
              size="large"
            ></el-input>
          </el-form-item>

          <el-form-item style="margin-bottom: 10px">
            <el-checkbox v-model="loginForm.rememberMe">记住我</el-checkbox>
          </el-form-item>

          <el-form-item style="width: 100%">
            <el-button :loading="loading" size="large" type="primary" style="width: 100%" @click="handleLogin">
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
          </el-form-item>
        </el-form>
        <!-- 滑动验证组件 -->
        <!-- v-if="isSliderVisible" 控制组件是否显示 -->
        <!-- @close 当用户点击关闭按钮时，隐藏滑动验证组件 -->
        <!-- @success 当验证成功时，触发 handleSuccess 方法 -->
        <slideVerify v-if="isSliderVisible" @close="isSliderVisible = false" @success="handleSuccess" />
      </div>
    </div>
  </div>
</template>

<script>
import { ElNotification } from 'element-plus'
import { apiGetBasicData_UserinfoLogin } from '../api/basic/basicDataUserinfoApi'
import Cookie from 'js-cookie'
import slideVerify from '../components/slideVerify.vue'

export default {
  name: 'LoginView',
  data() {
    return {
      isSliderVisible: false,
      loginForm: {
        JobNumber: '',
        passWord: '',
        rememberMe: false,
        uuid: ''
      },
      loginRules: {
        JobNumber: [{ required: true, trigger: 'blur', message: '账号不能为空' }],
        passWord: [{ required: true, trigger: 'blur', message: '密码不能为空' }]
      },
      loading: false,
      redirect: undefined
    }
  },

  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    // 获取用户名密码等Cookie
    this.getCookie()
    // 如果没有Cookie数据，使用默认值
    if (!this.loginForm.JobNumber) {
      this.loginForm.JobNumber = '230022802'
    }
    if (!this.loginForm.passWord) {
      this.loginForm.passWord = '123'
    }
    // token 过期提示
    this.point()
  },

  methods: {
    // 当滑动验证成功时的回调函数
    handleSuccess() {
      // 验证成功后隐藏滑动验证组件
      setTimeout(() => {
        this.isSliderVisible = false
        this.loading = true
        apiGetBasicData_UserinfoLogin(this.loginForm)
          .then((res) => {
            if (res.status === 200) {
              if (this.loginForm.rememberMe) {
                Cookie.set('JobNumber', this.loginForm.JobNumber)
                Cookie.set('passWord', this.loginForm.passWord)
                Cookie.set('rememberMe', this.loginForm.rememberMe)
              } else {
                Cookie.remove('JobNumber')
                Cookie.remove('passWord')
                Cookie.remove('rememberMe')
              }
              Cookie.set('token', res.data.data)
              setTimeout(() => {
                ElNotification({
                  title: this.getGreetingMessage(),
                  message: '欢迎登录MES',
                  type: 'success',
                  offset: 101,
                  duration: 1300
                })
                this.loading = false
                this.$router.push('/Home')
              }, 300)
            }
          })
          .catch(() => {
            this.loading = false
          })
      }, 600)
    },
    getCookie() {
      const JobNumber = Cookie.get('JobNumber')
      const passWord = Cookie.get('passWord')
      const rememberMe = Cookie.get('rememberMe')
      this.loginForm.JobNumber = JobNumber || ''
      this.loginForm.passWord = passWord || ''
      this.loginForm.rememberMe = rememberMe !== undefined ? Boolean(rememberMe) : false
    },
    getGreetingMessage() {
      const hours = new Date().getHours()
      if (hours < 12) {
        return '早上好'
      } else if (hours < 18) {
        return '下午好'
      } else {
        return '晚上好'
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.isSliderVisible = true
        } else {
          return false
        }
      })
    },
    point() {
      const point = Cookie.get('point') !== undefined
      if (point) {
        this.$notify({
          title: '提示',
          message: '当前登录状态已过期，请重新登录！',
          type: 'warning',
          duration: 5000
        })
        Cookie.remove('point')
      }
    }
  },
  // 注册自定义组件
  components: {
    slideVerify
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  height: 100vh;
  background-color: #0a0f1a;
  flex-direction: column;
}

.left-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 20px;
  background-color: #0a0f1a;
}

.slogan {
  color: #ffffff;
  text-align: center;
  margin-bottom: 20px;
}

.slogan h1 {
  font-size: 24px;
  font-weight: bold;
}

.slogan h2 {
  font-size: 18px;
}

.factory-image img {
  width: 100%;
  height: auto;
}

.right-panel {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  background-color: #0a0f1a;
}

.login-form {
  width: 100%;
  max-width: 320px;
  padding: 40px;
  background-color: rgba(255, 255, 255, 0.1);
  border-radius: 10px;
}

.login-form h2 {
  text-align: center;
  color: #ffffff;
  margin-bottom: 30px;
}

.el-input {
  background-color: rgba(255, 255, 255, 0.2);
  border: none;
  color: #ffffff;
}

.el-input::placeholder {
  color: #ccc;
}

.el-checkbox__label {
  color: #ffffff;
}

.login-button {
  width: 100%;
  background-color: #3a8ee6;
  border-color: #3a8ee6;
  color: white;
  height: 50px;
  border-radius: 8px;
}

.login-button:hover {
  background-color: #5aa0ee;
  border-color: #5aa0ee;
}

@media (min-width: 768px) {
  .login-container {
    flex-direction: row;
  }

  .right-panel {
    width: 50%;
  }
}

@media (min-width: 1024px) {
  .right-panel {
    width: 40%;
  }
}

@media (max-width: 767px) {
  .left-panel {
    display: none; /* 隐藏 left-panel */
  }

  .right-panel {
    width: 100%; /* 右侧内容全屏显示 */
  }
}
</style>
